package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utiles.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 体检套餐管理
 */

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    //使用JedisPool操作Redis服务
    @Autowired
    private JedisPool jedisPool;

    //文件上传
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        System.out.println(imgFile);
        String originalFilename = imgFile.getOriginalFilename();//原始文件名 3bd90d2c-4e82-42a1-a401-882c88b06a1a2.jpg
        int index = originalFilename.lastIndexOf(".");
        String extention = originalFilename.substring(index - 1);//.jpg
        String fileName = UUID.randomUUID().toString() + extention;//	FuM1Sa5TtL_ekLsdkYWcf5pyjKGu.jpg
        try {
            //将文件上传到七牛云服务器
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
        return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
    }

    @Reference
    private SetmealService setmealService;
    //新增套餐
    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        try{
            setmealService.add(setmeal,checkgroupIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return setmealService.pageQuery(queryPageBean);
    }
    // 根据id查询套餐
      @RequestMapping("findById")
    public Result findById(int id){
          try {
           Setmeal setmeal=setmealService.findById(id);
           return new Result ( true ,MessageConstant.ADD_SETMEAL_SUCCESR,setmeal); //查询套餐成功
          } catch (Exception e) {
              e.printStackTrace ();
              return new Result ( false,MessageConstant.ADD_SETMEAL_SUCCESB );//查询套餐失败
          }
      }
    //根据套餐ID查询当前套餐包含的检查组ID
    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        try {
            List<Integer> checkitemIds= setmealService.findCheckItemIdsByCheckGroupId(id);
            return new Result ( true,MessageConstant.QUERY_CHECKGROUP_SUCCESS );//查询检查组成功
        } catch (Exception e) {
            e.printStackTrace ();
            return  new Result ( false,MessageConstant.QUERY_CHECKGROUP_FAIL );//查询检查组失败
        }
    }
      //编辑套餐
      @RequestMapping("/edit")
      public Result edit(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
          try{
             setmealService.edit(setmeal,checkgroupIds);
          }catch (Exception e){
              e.printStackTrace();
              return new Result(false, MessageConstant.EDIT_CHECKGROUPX_FAIL);//编辑失败
          }
          return new Result(true,MessageConstant.EDIT_CHECKGROUU_SUCCESS);//编辑成功
      }
        /*删除套餐*/
        @RequestMapping("delete")
    public Result delete(Integer id){
            try {
                setmealService.delete(id);
            } catch (Exception e) {
                e.printStackTrace ();
                return new Result ( false,MessageConstant.DELETE_CHECKGROUO_FAIL );
            }
            return  new Result ( true, MessageConstant.DELETE_CHECKGROUK_SUCCESS );
        }

}
